home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / IDICT.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  23KB  |  670 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* idict.c */
  20. /* Dictionaries for Ghostscript */
  21. #include "ghost.h"
  22. #include "alloc.h"
  23. #include "errors.h"
  24. #include "iname.h"
  25. #include "packed.h"
  26. #include "save.h"            /* for value cache in names */
  27. #include "store.h"
  28. #include "iutil.h"            /* for array_get and obj_eq */
  29. #include "ivmspace.h"            /* for store check */
  30. #include "dict.h"            /* interface definition */
  31. #include "dstack.h"            /* ditto */
  32.  
  33. /*
  34.  * A dictionary is a structure of three elements (refs):
  35.  *
  36.  *    count - a t_integer whose value says how many entries are
  37.  *    occupied (N), and whose size says how many elements the client
  38.  *    thinks the dictionary can hold (C).  C may be less than M (see below).
  39.  *
  40.  *    keys - a t_shortarray or t_array of M+1 elements, containing
  41.  *    the keys.
  42.  *
  43.  *    values - a t_array of M+1 elements, containing the values.
  44.  *
  45.  * C < M is possible because on 32-bit systems, we round up M so that
  46.  * M is a power of 2; this allows us to use masking rather than division
  47.  * for computing the initial hash probe.  However, C is always the
  48.  * maxlength specified by the client, so clients get a consistent story.
  49.  */
  50. #define dict_round_size (!arch_ints_are_short)
  51. #if dict_round_size
  52. #  define hash_mod(hash, size) ((hash) & ((size) - 1))
  53. #else
  54. #  define hash_mod(hash, size) ((hash) % (size))
  55. #endif
  56. /*
  57.  * The first entry is always marked deleted, to reduce the cost of the
  58.  * wrap-around check.
  59.  *
  60.  * In the packed form:
  61.  *    unused entries contain packed_key_empty;
  62.  *    deleted entries contain packed_key_deleted.
  63.  * In the unpacked form:
  64.  *    unused entries contain a literal null;
  65.  *    deleted entries contain an executable null.
  66.  *
  67.  * Note that if the keys slot in the dictionary is new,
  68.  * all the key slots are new (more recent than the last save).
  69.  * We use this fact to avoid saving stores into packed keys
  70.  * for newly created dictionaries.
  71.  */
  72. #define dict_is_packed(dct) r_has_type(&(dct)->keys, t_shortarray)
  73. #define packed_key_empty (pt_tag(pt_integer) + 0)
  74. #define packed_key_deleted (pt_tag(pt_integer) + 1)
  75. #define packed_key_impossible pt_tag(pt_full_ref)    /* never matches */
  76. #define packed_name_key(nidx)\
  77.   ((nidx) <= packed_max_name_index ? pt_tag(pt_literal_name) + (nidx) :\
  78.    packed_key_impossible)
  79. /*
  80.  * Using a special mark for deleted entries causes lookup time to degrade
  81.  * as entries are inserted and deleted.  This is not a problem, because
  82.  * entries are almost never deleted.
  83.  */
  84. #define d_maxlength(dct) r_size(&(dct)->count)
  85. #define d_set_maxlength(dct,siz) r_set_size(&(dct)->count,siz)
  86. #define nslots(dct) r_size(&(dct)->values)
  87. #define npairs(dct) (nslots(dct) - 1)
  88. #define d_length(dct) ((uint)((dct)->count.value.intval))
  89.  
  90. /* Define the size of the largest valid dictionary. */
  91. /* This is limited by the size field of the keys and values refs, */
  92. /* and by the enumeration interface, which requires the size to */
  93. /* fit in an int. */
  94. const uint dict_max_size = max_ushort / 2 - 2;
  95.  
  96. /* Define whether dictionaries expand automatically when full. */
  97. int dict_auto_expand = 0;
  98.  
  99. /* Define the hashing function for names. */
  100. /* We don't have to scramble the index, because */
  101. /* indices are assigned in a scattered order (see name_ref in iname.c). */
  102. #define dict_name_index_hash(nidx) (nidx)
  103.  
  104. /* Define whether dictionaries are packed by default. */
  105. #define default_pack 1
  106.  
  107. /* Forward references */
  108. private int dict_create_contents(P3(uint size, dict *pdict, int pack));
  109.  
  110. /* Create a dictionary. */
  111. int
  112. dict_create(uint size, ref *pref)
  113. {    ref arr;
  114.     int code = alloc_array(&arr, a_all, sizeof(dict) / sizeof(ref), "dict_create");
  115.     dict *pdict = (dict *)arr.value.refs;
  116.     if ( code < 0 ) return code;
  117.     code = dict_create_contents(size, pdict, default_pack);
  118.     if ( code < 0 ) return code;
  119.     make_tav_new(pref, t_dictionary, a_all, pdict, pdict);
  120.     return 0;
  121. }
  122. private int
  123. dict_create_unpacked_keys(uint asize, dict *pdict)
  124. {    int code = alloc_array(&pdict->keys, a_all, asize, "dict_create(keys)");
  125.     ref *kp;
  126.     ref *zp;
  127.     register uint i;
  128.     if ( code < 0 ) return code;
  129.     ref_mark_new(&pdict->keys);
  130.     for ( zp = kp = pdict->keys.value.refs, i = asize; i; zp++, i-- )
  131.         make_null_new(zp);
  132.     r_set_attrs(kp, a_executable);    /* wraparound entry */
  133.     return 0;
  134. }
  135. private int
  136. dict_create_contents(uint size, dict *pdict, int pack)
  137. {    uint csize = (size == 0 ? 1 : size);    /* client-specified size */
  138.     uint asize = csize;
  139.     int code;
  140.     register uint i;
  141.     ref *zp;
  142. #if dict_round_size
  143.     /* Round up the actual allocated size to the next higher */
  144.     /* power of 2, so we can use & instead of %. */
  145.     while ( asize & (asize - 1) ) asize = (asize | (asize >> 1)) + 1;
  146. #endif
  147.     asize++;        /* allow room for wraparound entry */
  148.     code = alloc_array(&pdict->values, a_all, asize, "dict_create(values)");
  149.     if ( code < 0 ) return code;
  150.     ref_mark_new(&pdict->values);
  151.     for ( zp = pdict->values.value.refs, i = asize; i; zp++, i-- )
  152.         make_null_new(zp);
  153.     if ( pack )
  154.        {    uint ksize = (asize + packed_per_ref - 1) / packed_per_ref;
  155.         ref arr;
  156.         ref_packed *pkp;
  157.         ref_packed *pzp;
  158.         code = alloc_array(&arr, a_all, ksize, "dict_create(packed keys)");
  159.         if ( code < 0 ) return code;
  160.         pkp = (ref_packed *)arr.value.refs;
  161.         make_tasv_new(&pdict->keys, t_shortarray, a_all, asize,
  162.                   packed, pkp);
  163.         for ( pzp = pkp, i = 0; i < asize || i % packed_per_ref; pzp++, i++ )
  164.             *pzp = packed_key_empty;
  165.         *pkp = packed_key_deleted;    /* wraparound entry */
  166.        }
  167.     else                /* not packed */
  168.        {    int code = dict_create_unpacked_keys(asize, pdict);
  169.         if ( code < 0 ) return code;
  170.        }
  171.     make_tv_new(&pdict->count, t_integer, intval, 0);
  172.     d_set_maxlength(pdict, csize);
  173.     return 0;
  174. }
  175.  
  176. /*
  177.  * Define a macro for searching a packed dictionary.  Free variables:
  178.  *    ref_packed kpack - holds the packed key.
  179.  *    uint hash - holds the hash of the name.
  180.  *    dict *pdict - points to the dictionary.
  181.  *    uint size - holds npairs(pdict).
  182.  * Note that the macro is *not* enclosed in {}, so that we can access
  183.  * the values of kbot and kp after leaving the loop.
  184.  *
  185.  * We break the macro into two to avoid overflowing some preprocessors.
  186.  */
  187. #define packed_search_1(del,pre,post,miss)\
  188.    const ref_packed *kbot = pdict->keys.value.packed;\
  189.    register const ref_packed *kp;\
  190.    for ( kp = kbot + hash_mod(hash, size) + 2; ; )\
  191.     { if ( *--kp == kpack )\
  192.        { pre (pdict->values.value.refs + (kp - kbot));\
  193.      post;\
  194.        }\
  195.       else if ( !packed_ref_is_name(kp) )\
  196.        { /* Empty, deleted, or wraparound. Figure out which. */\
  197.      if ( *kp == packed_key_empty ) miss;\
  198.      if ( kp == kbot ) break;    /* wrap */\
  199.      else { del; }\
  200.        }\
  201.     }
  202. #define packed_search_2(del,pre,post,miss)\
  203.    for ( kp += size + 1; ; )\
  204.     { if ( *--kp == kpack )\
  205.        { pre (pdict->values.value.refs + (kp - kbot));\
  206.      post;\
  207.        }\
  208.       else if ( !packed_ref_is_name(kp) )\
  209.        { /* Empty, deleted, or wraparound. Figure out which. */\
  210.      if ( *kp == packed_key_empty ) miss;\
  211.      if ( kp == kbot ) break;    /* wrap */\
  212.      else { del; }\
  213.        }\
  214.     }
  215.  
  216. /*
  217.  * Look up in a stack of dictionaries.  Store a pointer to the value slot
  218.  * where found, or to the (value) slot for inserting.
  219.  * Return 1 if found, 0 if not and there is room for a new entry in
  220.  * the top dictionary on the stack, or e_dictfull if the top dictionary
  221.  * is full and the key is missing.
  222.  * Note that pdbot <= pdtop, and the search starts at pdtop.
  223.  */
  224. int
  225. dict_lookup(const ref *pdbot, const ref *pdtop, const ref *pkey,
  226.   ref **ppvalue /* result is stored here */)
  227. {    const ref *pdref = pdtop;
  228.     uint nidx;
  229.     ref_packed kpack;
  230.     uint hash;
  231.     int ktype;
  232.     int full = 1;            /* gets set to 0 or e_dictfull */
  233.     /* Compute hash.  The only types we bother with are strings, */
  234.     /* names, and (unlikely, but worth checking for) integers. */
  235.     switch ( r_type(pkey) )
  236.        {
  237.     case t_name:
  238.         nidx = name_index(pkey);
  239. nh:        hash = dict_name_index_hash(nidx);
  240.         kpack = packed_name_key(nidx);
  241.         ktype = t_name;
  242.         break;
  243.     case t_string:            /* convert to a name first */
  244.        {    ref nref;
  245.         int code = name_ref(pkey->value.bytes,
  246.                     r_size(pkey), &nref, 1);
  247.         if ( code < 0 ) return code;
  248.         nidx = name_index(&nref);
  249.        }    goto nh;
  250.     case t_integer:
  251.         hash = (uint)pkey->value.intval * 30503;
  252.         kpack = packed_key_impossible;
  253.         ktype = -1;
  254.         break;
  255.     default:
  256.         hash = r_btype(pkey) * 99;    /* yech */
  257.         kpack = packed_key_impossible;
  258.         ktype = -1;
  259.        }
  260.     do
  261.        {    dict *pdict = pdref->value.pdict;
  262.         uint size = npairs(pdict);
  263.         register int etype;
  264.         /* Search the dictionary */
  265.         if ( dict_is_packed(pdict) )
  266.            {    const ref_packed *pslot = 0;
  267.             packed_search_1(if ( pslot == 0 ) pslot = kp,
  268.                     *ppvalue =, return 1, goto miss);
  269.             packed_search_2(if ( pslot == 0 ) pslot = kp,
  270.                     *ppvalue =, return 1, goto miss);
  271.             /* Double wraparound. */
  272.             /* Set full = e_dictfull if first dict and */
  273.             /* dict is full (pslot == 0). */
  274.             if ( full > 0 )    /* first dictionary */
  275.                {    if ( pslot == 0 )
  276.                   full = e_dictfull;
  277.                 else
  278.                 { *ppvalue = pdict->values.value.refs +
  279.                     (pslot - kbot),
  280.                   full = 0;
  281.                 }
  282.                }
  283.             goto next_dict;
  284. miss:            /* Key is missing, not double wrap. */
  285.             if ( full > 0 )    /* first dictionary */
  286.                {    if ( pslot == 0 ) pslot = kp;
  287.                 *ppvalue = pdict->values.value.refs +
  288.                   (pslot - kbot),
  289.                 full = 0;
  290.                }
  291.            }
  292.         else
  293.            {    ref *kbot = pdict->keys.value.refs;
  294.             register ref *kp;
  295.             ref *pslot = 0;
  296.             int wrap = 0;
  297.             for ( kp = kbot + hash_mod(hash, size) + 2; ; )
  298.                {    --kp;
  299.                 if ( (etype = r_type(kp)) == ktype )
  300.                    {    /* Fast comparison if both keys are names */
  301.                     if ( name_index(kp) == nidx )
  302.                        {    *ppvalue = pdict->values.value.refs + (kp - kbot);
  303.                         return 1;
  304.                        }
  305.                    }
  306.                 else if ( etype == t_null )
  307.                    {    /* Empty, deleted, or wraparound. */
  308.                     /* Figure out which. */
  309.                     if ( kp == kbot )    /* wrap */
  310.                        {    if ( wrap++ )    /* wrapped twice */
  311.                            {    if ( full > 0 )
  312.                                {    if ( pslot != 0 )
  313.                                     break;
  314.                                 full = e_dictfull;
  315.                                }
  316.                             goto next_dict;
  317.                            }
  318.                         kp += size + 1;
  319.                        }
  320.                     else if ( r_has_attr(kp, a_executable) )
  321.                        {    /* Deleted entry, save the slot. */
  322.                         if ( pslot == 0 ) pslot = kp;
  323.                        }
  324.                     else    /* key not found */
  325.                         break;
  326.                    }
  327.                 else
  328.                    {    if ( obj_eq(kp, pkey) )
  329.                        {    *ppvalue = pdict->values.value.refs + (kp - kbot);
  330.                         return 1;
  331.                        }
  332.                    }
  333.                }
  334.             if ( full > 0 )
  335.                {    *ppvalue = pdict->values.value.refs +
  336.                   ((pslot != 0 ? pslot : kp) - kbot);
  337.                 full = 0;
  338.                }
  339.            }
  340. next_dict: ;
  341.        }
  342.     while ( --pdref >= pdbot );
  343.     return full;
  344. }
  345.  
  346. /*
  347.  * Look up a name on the dictionary stack.
  348.  * Return the pointer to the value if found, 0 if not.
  349.  * This is just an optimization of dict_lookup with a different interface.
  350.  */
  351. ref *
  352. dict_find_name_by_index(uint nidx)
  353. {    ds_ptr pdref = dsp;
  354. /* Since we know the hash function is the identity function, */
  355. /* there's no point in allocating a separate variable for it. */
  356. #define hash dict_name_index_hash(nidx)
  357.     ref_packed kpack = packed_name_key(nidx);
  358.     do
  359.        {    dict *pdict = pdref->value.pdict;
  360.         uint size = npairs(pdict);
  361.         if ( dict_is_packed(pdict) )
  362.            {    packed_search_1(0, return, 0, goto miss);
  363.             packed_search_2(0, return, 0, break);
  364. miss:            ;
  365.            }
  366.         else
  367.            {    ref *kbot = pdict->keys.value.refs;
  368.             register ref *kp;
  369.             int wrap = 0;
  370.             /* Search the dictionary */
  371.             for ( kp = kbot + hash_mod(hash, size) + 2; ; )
  372.                {    --kp;
  373.                 if ( r_has_type(kp, t_name) )
  374.                    {    if ( name_index(kp) == nidx )
  375.                       return pdict->values.value.refs +
  376.                         (kp - kbot);
  377.                    }
  378.                 else if ( r_has_type(kp, t_null) )
  379.                    {    /* Empty, deleted, or wraparound. */
  380.                     /* Figure out which. */
  381.                     if ( !r_has_attr(kp, a_executable) )
  382.                         break;
  383.                     if ( kp == kbot )    /* wrap */
  384.                        {    if ( wrap++ )
  385.                             break;    /* 2 wraps */
  386.                         kp += size + 1;
  387.                        }
  388.                    }
  389.                }
  390.            }
  391.        }
  392.     while ( --pdref >= dsbot );
  393.     return (ref *)0;
  394. #undef hash
  395. }
  396.  
  397. /*
  398.  * Enter a key-value pair in a dictionary.
  399.  * The caller is responsible for ensuring key is not a null.
  400.  * Return 0, e_dictfull, or e_VMerror if the key was a string
  401.  * and a VMerror occurred when converting it to a name.
  402.  */
  403. int
  404. dict_put(ref *pdref /* t_dictionary */, const ref *pkey, const ref *pvalue)
  405. {    ref *pvslot;
  406. top:    if ( dict_find(pdref, pkey, &pvslot) <= 0 )    /* not found */
  407.        {    /* Check for overflow */
  408.         dict *pdict = pdref->value.pdict;
  409.         ref kname;
  410.         uint index = pvslot - pdict->values.value.refs;
  411.         if ( d_length(pdict) == d_maxlength(pdict) )
  412.            {    int code;
  413.             ulong new_size;
  414.             if ( !dict_auto_expand )
  415.                 return_error(e_dictfull);
  416.             /* We might have maxlength < npairs, if */
  417.             /* dict_round_size is true. */
  418.             new_size = (ulong)npairs(pdict) * 3 / 2 + 2;
  419.             if ( new_size > dict_max_size )
  420.                {    if ( d_maxlength(pdict) == dict_max_size )
  421.                     return_error(e_dictfull);
  422.                 new_size = dict_max_size;
  423.                }
  424.             if ( new_size > npairs(pdict) )
  425.             {    code = dict_resize(pdref, (uint)new_size);
  426.                 if ( code < 0 ) return code;
  427.             }
  428.             else
  429.             {    /* maxlength < npairs, we can grow in place */
  430.                 ref_save(&pdict->count, "dict_put(size)");
  431.                 d_set_maxlength(pdict, npairs(pdict));
  432.             }
  433.             goto top;    /* keep things simple */
  434.            }
  435.         /* If the key is a string, convert it to a name. */
  436.         if ( r_has_type(pkey, t_string) )
  437.            {    int code = name_from_string(pkey, &kname);
  438.             if ( code < 0 ) return code;
  439.             pkey = &kname;
  440.            }
  441.         if ( dict_is_packed(pdict) )
  442.            {    ref_packed *kp;
  443.             if ( !r_has_type(pkey, t_name) ||
  444.                  name_index(pkey) > packed_max_name_index
  445.                )
  446.                {    /* Change to unpacked representation. */
  447.                 /* We can't just use dict_resize, */
  448.                 /* because the values slots mustn't move. */
  449.                 uint count = nslots(pdict);
  450.                 const ref_packed *okp =
  451.                     pdict->keys.value.packed;
  452.                 ref old_keys;
  453.                 int code;
  454.                 ref *nkp;
  455.                 make_array(&old_keys, 0,
  456.                       (count + packed_per_ref - 1) /
  457.                         packed_per_ref,
  458.                       (ref *)okp);
  459.                 if ( alloc_save_new_mask )
  460.                     alloc_save_change(&pdict->keys, "dict_unpack(keys)");
  461.                 code = dict_create_unpacked_keys(count, pdict);
  462.                 if ( code < 0 ) return code;
  463.                 for ( nkp = pdict->keys.value.refs; count--; okp++, nkp++ )
  464.                   if ( packed_ref_is_name(okp) )
  465.                     packed_get(okp, nkp);
  466.                 alloc_free_array(&old_keys,
  467.                          "dict_unpack(old keys)");
  468.                 return dict_put(pdref, pkey, pvalue);
  469.                }
  470.             kp = (ref_packed *)(pdict->keys.value.packed + index);
  471.             if ( alloc_save_new_mask &&
  472.                  !r_has_attr(&pdict->keys, l_new)
  473.                )
  474.                {    /* See initial comment for why it is safe */
  475.                 /* not to save the change if the keys */
  476.                 /* array itself is new. */
  477.                 alloc_save_change(pdict->keys.value.refs + (index / packed_per_ref), "dict_put(key)");
  478.                }
  479.             *kp = pt_tag(pt_literal_name) + name_index(pkey);
  480.            }
  481.         else
  482.            {    ref *kp = pdict->keys.value.refs + index;
  483.             if_debug2('d', "[d]%lx fill key %lx\n",
  484.                   (ulong)pdict, (ulong)kp);
  485.             if ( !r_is_global(pkey) && r_is_global(pdref) )
  486.                 return_error(e_invalidaccess);
  487.             ref_assign_old(kp, pkey, "dict_put(key)");    /* set key of pair */
  488.            }
  489.         ref_save(&pdict->count, "dict_put(count)");
  490.         pdict->count.value.intval++;
  491.         /* If the key is a name, update its 1-element cache. */
  492.         if ( r_has_type(pkey, t_name) )
  493.            {    name *pname = pkey->value.pname;
  494.             if ( pname->pvalue == pv_no_defn &&
  495.                 (pdict == systemdict->value.pdict ||
  496.                  pdict == userdict->value.pdict) &&
  497.                 /* Only set the cache if we aren't inside */
  498.                 /* a save.  This way, we never have to */
  499.                 /* undo setting the cache. */
  500.                 alloc_save_level() == 0
  501.                )
  502.                {    /* Set the cache */
  503.                 pname->pvalue = pvslot;
  504.                }
  505.             else    /* The cache is worthless */
  506.                 pname->pvalue = pv_other;
  507.            }
  508.        }
  509.     if_debug6('d', "[d]in %lx put %lx: %lx %lx -> %lx %lx\n",
  510.           (ulong)pdref->value.pdict, (ulong)pvslot,
  511.           ((ulong *)pvslot)[0], ((ulong *)pvslot)[1],
  512.           ((ulong *)pvalue)[0], ((ulong *)pvalue)[1]);
  513.     /* Check the value. */
  514.     if ( !r_is_global(pvalue) && r_is_global(pdref) )
  515.         return_error(e_invalidaccess);
  516.     ref_assign_old(pvslot, pvalue, "dict_put(value)");
  517.     return 0;
  518. }
  519.  
  520. /* Remove an element from a dictionary. */
  521. int
  522. dict_undef(ref *pdref, const ref *pkey)
  523. {    ref *pvslot;
  524.     dict *pdict;
  525.     uint index;
  526.     if ( dict_find(pdref, pkey, &pvslot) <= 0 )
  527.         return_error(e_undefined);
  528.     /* Remove the entry from the dictionary. */
  529.     pdict = pdref->value.pdict;
  530.     index = pvslot - pdict->values.value.refs;
  531.     if ( dict_is_packed(pdict) )
  532.        {    ref_packed *pkp =
  533.            (ref_packed *)(pdict->keys.value.packed + index);
  534.         /* Since packed arrays don't have room for a saved bit, */
  535.         /* always save the entire ref containing this key. */
  536.         /* This wastes a little space, but undef is rare. */
  537.         /* See the initial comment for why it is safe not to save */
  538.         /* the change if the keys array itself is new. */
  539.         if ( alloc_save_new_mask && !r_has_attr(&pdict->keys, l_new) )
  540.             alloc_save_change(pdict->keys.value.refs + (index / packed_per_ref), "dict_undef(key)");
  541.         /* Accumulating deleted entries slows down lookup. */
  542.         /* Detect the easy case where we can use an empty entry */
  543.         /* rather than a deleted one, namely, when the next entry */
  544.         /* in the probe order is empty. */
  545.         if ( pkp[-1] == packed_key_empty )
  546.             *pkp = packed_key_empty;
  547.         else
  548.             *pkp = packed_key_deleted;
  549.        }
  550.     else                /* not packed */
  551.        {    ref *kp = pdict->keys.value.refs + index;
  552.         make_null_old(kp, "dict_undef(key)");
  553.         /* Accumulating deleted entries slows down lookup. */
  554.         /* Detect the easy case where we can use an empty entry */
  555.         /* rather than a deleted one, namely, when the next entry */
  556.         /* in the probe order is empty. */
  557.         if ( !r_has_type(kp - 1, t_null) ||    /* full entry */
  558.              r_has_attr(kp - 1, a_executable)    /* deleted or wraparound */
  559.             )
  560.             r_set_attrs(kp, a_executable);    /* mark as deleted */
  561.        }
  562.     ref_save(&pdict->count, "dict_undef(count)");
  563.     pdict->count.value.intval--;
  564.     /* If the key is a name, update its 1-element cache. */
  565.     if ( r_has_type(pkey, t_name) )
  566.        {    name *pname = pkey->value.pname;
  567.         if ( pv_valid(pname->pvalue) &&
  568.             (pdict == systemdict->value.pdict ||
  569.              pdict == userdict->value.pdict) )
  570.            {    /* Clear the cache */
  571.             pname->pvalue = pv_no_defn;
  572.            }
  573.        }
  574.     make_null_old(pvslot, "dict_undef(value)");
  575.     return 0;
  576. }
  577.  
  578. /* Return the number of elements in a dictionary. */
  579. uint
  580. dict_length(const ref *pdref /* t_dictionary */)
  581. {    return d_length(pdref->value.pdict);
  582. }
  583.  
  584. /* Return the capacity of a dictionary. */
  585. uint
  586. dict_maxlength(const ref *pdref /* t_dictionary */)
  587. {    return d_maxlength(pdref->value.pdict);
  588. }
  589.  
  590. /* Copy one dictionary into another. */
  591. int
  592. dict_copy(const ref *pdrfrom /* t_dictionary */, ref *pdrto /* t_dictionary */)
  593. {    int index = dict_first(pdrfrom);
  594.     ref elt[2];
  595.     int code;
  596.     while ( (index = dict_next(pdrfrom, index, elt)) >= 0 )
  597.       if ( (code = dict_put(pdrto, &elt[0], &elt[1])) < 0 )
  598.         return code;
  599.     return 0;
  600. }
  601.  
  602. /* Resize a dictionary. */
  603. int
  604. dict_resize(ref *pdrfrom, uint new_size)
  605. {    dict *pdict = pdrfrom->value.pdict;
  606.     uint count = nslots(pdict);
  607.     dict dnew;
  608.     ref drto;
  609.     int code;
  610.     uint local;
  611.     if ( new_size < d_length(pdict) )
  612.        {    if ( !dict_auto_expand )
  613.             return_error(e_dictfull);
  614.         new_size = d_length(pdict);
  615.        }
  616.     local = alloc_select_local(r_local(pdrfrom));
  617.     if ( (code = dict_create_contents(new_size, &dnew, dict_is_packed(pdict))) < 0 )
  618.     {    alloc_select_local(local);
  619.         return code;
  620.     }
  621.     make_tav_new(&drto, t_dictionary, a_all, pdict, &dnew);
  622.     dict_copy(pdrfrom, &drto);    /* can't fail */
  623.     /* Free the old dictionary */
  624.     alloc_free_array(&pdict->values, "dict_resize(old values)");
  625.     if ( dict_is_packed(pdict) )
  626.     {    /* We reset the size so alloc_free_array will know */
  627.         /* how big the keys are in refs.... */
  628.         r_set_size(&pdict->keys,
  629.                (count + packed_per_ref - 1) / packed_per_ref);
  630.     }
  631.     alloc_free_array(&pdict->keys, "dict_resize(old keys)");
  632.     /* ... but now we have to reset it, so that if we are in a save, */
  633.     /* the correct value gets saved by ref_assign_old. */
  634.     r_set_size(&pdict->keys, count);
  635.     ref_assign_old(&pdict->keys, &dnew.keys, "dict_resize(keys)");
  636.     ref_assign_old(&pdict->values, &dnew.values, "dict_resize(values)");
  637.     ref_save(&pdict->count, "dict_resize(size)");
  638.     d_set_maxlength(pdict, new_size);
  639.     alloc_select_local(local);
  640.     return 0;
  641. }
  642.  
  643. /* Prepare to enumerate a dictionary. */
  644. int
  645. dict_first(const ref *pdref)
  646. {    return (int)nslots(pdref->value.pdict);
  647. }
  648.  
  649. /* Enumerate the next element of a dictionary. */
  650. int
  651. dict_next(const ref *pdref, int index, ref *eltp /* ref eltp[2] */)
  652. {    dict *pdict = pdref->value.pdict;
  653.     ref *vp = pdict->values.value.refs + index;
  654.     while ( vp--, --index >= 0 )
  655.        {    array_get(&pdict->keys, (long)index, eltp);
  656.         /* Make sure this is a valid entry. */
  657.         if ( r_has_type(eltp, t_name) ||
  658.              (!dict_is_packed(pdict) && !r_has_type(eltp, t_null))
  659.            )
  660.            {    eltp[1] = *vp;
  661.             if_debug6('d', "[d]%lx index %d: %lx %lx, %lx %lx\n",
  662.                 (ulong)pdict, index,
  663.                 ((ulong *)eltp)[0], ((ulong *)eltp)[1],
  664.                 ((ulong *)vp)[0], ((ulong *)vp)[1]);
  665.             return index;
  666.            }
  667.        }
  668.     return -1;            /* no more elements */
  669. }
  670.